from django.db import models
from utils.modelsMixin import ModelSetMixin
from django.contrib.auth.models import User  # auth自带的用户表

# Create your models here.
class UserDetail(ModelSetMixin):
    SEX_CHOICES = (
        (0,'女'),
        (1,'男')
    )
    avatar = models.TextField(null=True,blank=True,verbose_name='头像')  # 长文本类型
    phone = models.CharField(null=True,blank=True,verbose_name='手机号',max_length=11,unique=True)
    age = models.IntegerField(verbose_name='年龄',null=True,blank=True)
    sex = models.IntegerField(verbose_name='性别',null=True,blank=True,
                              choices=SEX_CHOICES)
    birthday = models.DateField(null=True,blank=True,verbose_name='生日')

    # 外键字段 一对一联系
    user = models.OneToOneField(User,on_delete=models.DO_NOTHING)  # 无任何级联删除方式

    class Meta:
        db_table = 'user_detail'  # 设置表名
        verbose_name = '用户详情'
        verbose_name_plural = verbose_name


# 行政区模型类
class Area(models.Model):
    name = models.CharField(max_length=30, verbose_name='名称')
    parent = models.ForeignKey('self',on_delete=models.SET_NULL,null=True,blank=True,verbose_name='上级行政区')

    class Meta:
        db_table = 'area'  # 设置表名
        verbose_name = '行政区划'
        verbose_name_plural = verbose_name

# 收货地址
class Address(ModelSetMixin):
    name = models.CharField(max_length=40, verbose_name='地址名')
    receiver = models.CharField(max_length=40, verbose_name='收货人')
    province = models.ForeignKey(Area,on_delete=models.PROTECT,verbose_name='省',related_name='province_addresses')
    city = models.ForeignKey(Area,on_delete=models.PROTECT,verbose_name='市',related_name='city_addresses')
    district = models.ForeignKey(Area,on_delete=models.PROTECT,verbose_name='区',related_name='district_addresses')
    place = models.CharField(max_length=40, verbose_name='详情地址')
    mobile = models.CharField(max_length=11, verbose_name='手机号')

    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户')

    class Meta:
        ordering = ['-update_time']
        db_table = 'address'  # 设置表名
        verbose_name = '收货地址'
        verbose_name_plural = verbose_name



